# -*- coding: utf-8 -*- 
# 作者:飞虎
# 开发时间:2022/5/5 14:29
from PyQt5.Qt import *

# class AccountTool():
# 	self.ACCOUNT_ERROR=0
# 	PWD_ERROR=1
# 	SUCCESS=2
# 	def check_login(self,account,pwd):
# 		# 把密码和账号发送给服务器,等待服务器返回结果
# 		if account != 'sz':
# 			return self.ACCOUNT_ERROR


class Window(QWidget):
	def __init__(self):
		super().__init__()
		self.setWindowTitle('QLineEdit案例学习')
		self.resize(500, 500)
		self.setMinimumSize(400,400)
		self.setup_ui()

	def setup_ui(self):
		# 添加三个控件
		self.account_le = QLineEdit(self)
		self.pwd_le = QLineEdit(self)
		self.pwd_le.setEchoMode(QLineEdit.Password)
		self.login_btn = QPushButton(self)
		self.login_btn.setText('登  录')

		# 占位文本的提示
		self.account_le.setPlaceholderText('请输入账号')
		self.pwd_le.setPlaceholderText('请输入密码')

		# 设置密码文本框,自动显示清空按钮
		self.pwd_le.setClearButtonEnabled(True)

		# 添加自定义行为操作 明文和密文的切换





		action = QAction(self.pwd_le)
		action.setIcon(QIcon('close.png'))
		def change():
			if self.pwd_le.echoMode() == QLineEdit.Normal:
				self.pwd_le.setEchoMode(QLineEdit.Password)
				action.setIcon(QIcon('close.png'))
			else:
				self.pwd_le.setEchoMode(QLineEdit.Normal)
				action.setIcon(QIcon('open.png'))

		action.triggered.connect(change)
		self.pwd_le.addAction(action,QLineEdit.TrailingPosition)
		# self.pwd_le.addAction(action,QLineEdit.LeadingPosition)



		self.login_btn.clicked.connect(self.login_cao)

		completer =QCompleter(['Sz','shunzi','wangzha'],self.account_le)

		self.account_le.setCompleter(completer)

	def login_cao(self):
		# 获取账号和密码信息
		account = self.account_le.text()
		pwd = self.pwd_le.text()

		# state=AccountTool.check_login(account,pwd)

		# print(account,pwd)
		if account != 'sz':
			print('账号错误')
			self.account_le.setText("")
			self.pwd_le.setText("")
			self.account_le.setFocus()
			return None
		if pwd != 'itlike':
			print('密码错误')
			self.pwd_le.setText("")
			self.pwd_le.setFocus()
			return None


		print('登录成功')

		# if account == 'sz':
		# 	if pwd == 'itlike':
		# 		print('登录成功')
		# 	else:
		# 		print('密码错误')
		# 		self.pwd_le.setText("")
		# else:
		# 	print('账号错误')
		# 	self.account_le.setText("")
		# 	self.pwd_le.setText("")
		# 	self.account_le.setFocus()



	def resizeEvent(self,evt):
		widget_width = 150
		widget_height = 40
		margin = 60

		self.account_le.resize(widget_width, widget_height)
		self.pwd_le.resize(widget_width, widget_height)
		self.login_btn.resize(widget_width, widget_height)

		x = int((self.width() - widget_width) / 2)
		self.account_le.move(x, int(self.height() / 5))
		self.pwd_le.move(x, self.account_le.y() + widget_height + margin)
		self.login_btn.move(x, self.pwd_le.y() + widget_height + margin)

if __name__ == '__main__':
	import sys

	app = QApplication(sys.argv)
	window = Window()
	window.show()
	sys.exit(app.exec_())
